home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / Parser 3 / Info < prev    next >
Encoding:
Text File  |  1994-06-08  |  5.5 KB  |  113 lines  |  [TEXT/DEDL]

  1.                               •••••••••••••••••••••••••••••                                 
  2.                               SCANNER, PARSER AND EVALUATOR                                 
  3.                               •••••••••••••••••••••••••••••                                 
  4.                                                                                             
  5. INTRO:
  6. ------
  7.  
  8. This program can read, analyse and evaluate any mathematical expression that's given in so
  9. called Infix notation. This is no normal way of writing math expressions. You can supply it
  10. with a value for the variable x and the expression will be evaluated correctly.
  11.  
  12. This is very useful, if you are programming a plot program and want to be able to let the
  13. program evaluate the curve plots through functions that are not pre defined in the program
  14. code but can be established during the runtime of the program. There are also many other
  15. examples where such function parsers become very useful. When you compile any program with
  16. your BASIC compiler, it does just the same sort of scanning expressions and parsing them.
  17.  
  18. Look at the programming code to see how this is done in simple ZBASIC or FUTURE BASIC. I
  19. have used an intrinsic recursive method. Intrinsic, because you have to examine the code a
  20. little while, before you see the recursion.
  21.  
  22. You can use this code free and you can change it in any way you like, but please post me a
  23. message and your own code on Compuserve, if you made your own useful changes to this code.
  24.  
  25. One variable x can be used. All operators +, -, *, /, ^ and many math functions are supported.
  26. If not implemented logical functions yet, allthough it's possible to do that. Perhaps you
  27. would like to add this type of functions.
  28.  
  29. You can write in small, in capital letter or both mixed up. All three types of brackets can
  30. be used. If you write π or e the program will change it to the appropriate values of pi and
  31. the euler constant. You may use any number spaces, they will be skipped, when the program
  32. parses the expression.
  33.  
  34. What makes this parser a little bit special (beside the unique way of implementing it in
  35. BASIC) is the fact, that you can ommit the multiplication sign "*" in allmost any cases.
  36. As a rule: You can always ommit "*" when normal mathematical convention also allows it.
  37.  
  38. a) between value (left) and x:                          2x    is    2*x,                 
  39. b) between x and x:                                     xx    is    x*x,                 
  40. c) between value (left) and bracket:                2(x-4)    is    2*(x-4),             
  41. d) between x and bracket (both sides):    x(3-x) or (3-x)x    is    x*(3-x) or (3-x)*x      
  42. e) between bracket and bracket:                 (x-2)(4+x)    is    (x-2)*(4+x)          
  43. f) between value (left) and funktion:              4sin(x)    is    4*sin(x)             
  44. g) between x and function (both sides): xexp(x) or exp(x)x    is    x*exp(x) or exp(x)*x    
  45. h) between bracket (left) and function:        (x-1)cos(x)    is    (x-1)*cos(x)         
  46. i) between functions:                          sin(x)ln(x)    is    sin(x)*ln(x)         
  47. Negative Exponenten don't need brackets:              x^-3    is    x^(-3)               
  48.  
  49. In the following cases you must use the "*" operator:
  50.  
  51. a) between brackets(left) and values:              (x-4)*3
  52. b) between values:                                     3*4
  53. c) between x (left) and values:                        x*2
  54. d) between functions (left) and values:           tan(x)*5
  55.  
  56. Error routines are implemented, but only very rough. So it's your task to add your special
  57. error trpping code.
  58.  
  59.  
  60. I hope this is useful for you. It's my first greater attempt in parsing technics and it
  61. took me a while to get it work like it should.
  62. So, please give me some response if you like it or if your have any further suggestions,
  63. questions. I'm also very pleased, if you would inform me about any possible bug in this
  64. program.
  65.  
  66.  
  67. COMPILING:
  68. ----------
  69. First start ZBASIC or FUTURE BASIC with this code and make shure, that you have changed
  70. the preferences to the following values:
  71.  
  72. Default Variable Type:     Integer
  73. Convert to Upper Case:     Yes
  74. Space Req.After Key Words: Yes
  75. Array Base 1:              Yes
  76. Type:                      APPL
  77. Creator:                   TERM
  78.  
  79. It's essential to use this program identifier together with the resource file "Terme.Res".
  80. Look for the line with: Res%=FN OPENRESFILE... and deactivate it.
  81. After that simply compile this program. After that load "Terme.Res" into ResEdit and also
  82. open the new program "Terme". Then delete all the "DITL" and "DLOG" resources from the
  83. program, because you don't need them here and paste all the resources from "Terme.Res" to
  84. the program. That's it!
  85.  
  86. You can now start "Terme" and try some math expressions. Please notice, that you don't need
  87. to write it in the form "y=x+4....", simply write down the right hand side of this function.
  88. If you don't give in any value for the variable x then zero is choosen automatically.
  89.  
  90. If you give in invalid characters like "!" or "#", the program will give you an error
  91. warning and skip immediately skip that character. You may use "." or "," as comma, because
  92. it will instantaniously be converted to "." anyway. You can easily implement more error
  93. sophisticated trapping while writing expressions or values.
  94.  
  95. After some milliseconds you get the value of the function for this x value.
  96.  
  97.  
  98. END:
  99. ----
  100. If you want to contact me, here is my address and user id#:
  101.  
  102. Detlef Reimers
  103. Germany
  104. 22457 Hamburg
  105. Süntelstraße 7
  106. Tel: 049-40-559-2773
  107. Compuserve#: 10015,1146
  108.  
  109. Thank you for your interest and good look!
  110.  
  111. 4. June 1994
  112. Detlef Reimers
  113.